package com.rnkrsoft.suyan.config.server.ajax.data;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javax.doc.data.IResponse;

public class AjaxResponse {
    /**
     * 通信层应答代码
     */
    String code;
    /**
     * 通信层应答描述
     */
    String desc;
    /**
     * 业务数据JSON字符串
     */
    Object data;

    public AjaxResponse(String code, String desc, Object data) {
        this.code = code;
        this.desc = desc;
        this.data = data;
    }


    public String getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

    public Object getData() {
        return data;
    }

    @Override
    public String toString() {
        return "AjaxResponse{" +
                "code='" + code + '\'' +
                ", desc='" + desc + '\'' +
                ", data='" + data + '\'' +
                '}';
    }

    /**
     * 创建Ajax应答建造器
     * @return
     */
    public static AjaxResponseBuilder builder(){
        return new AjaxResponseBuilder();
    }

    public static class AjaxResponseBuilder{
        /**
         * 通信层应答代码
         */
        String code;

        /**
         * 通信层应答描述
         */
        String desc;
        /**
         * 业务数据JSON字符串
         */
        IResponse data;

        public AjaxResponseBuilder code(String code, String desc) {
            this.code = code;
            this.desc = desc;
            return this;
        }

        public AjaxResponseBuilder success(){
            this.code = "200";
            this.desc = "成功";
            return this;
        }

        public AjaxResponseBuilder failure(){
            this.code = "500";
            this.desc = "失败";
            return this;
        }

        public AjaxResponseBuilder noAuthority(){
            this.code = "403";
            this.desc = "无权限";
            return this;
        }
        public AjaxResponseBuilder data(IResponse data) {
            this.data = data;
            return this;
        }

        public AjaxResponse build(){
            AjaxResponse response = new AjaxResponse(code, desc, data);
            return response;
        }
    }
}
